package com.zhiyun.service.impl;

import com.zhiyun.domain.Course;
import com.zhiyun.domain.Quickresponse;
import com.zhiyun.mapper.QuickresponseMapper;
import com.zhiyun.service.QuickResponseService;
import com.zhiyun.util.Db;
import com.zhiyun.vo.Result;

import java.util.List;

/**
 * @author zc
 * @Date 2025/4/29 下午3:30
 */
public class QuickResponseServiceImpl implements QuickResponseService {
    QuickresponseMapper quickresponseMapper = Db.getMapper(QuickresponseMapper.class);
    @Override
    public Result myQuickResponses(int loginUserId, int courseId) {
        List<Quickresponse> list = quickresponseMapper.selectByUserAndCourse(loginUserId,courseId);
        return Result.ok(list);
    }

    @Override
    public Result quickResponses(int id, int studentId) {
        Quickresponse quickresponse = quickresponseMapper.selectByPrimaryKey(id);
        quickresponse.setStudentId(studentId);
        int rows = quickresponseMapper.updateByPrimaryKeyWithNull(quickresponse);
        if (rows > 0) {
            return Result.okMsg("成功抢答");
        }else {
            return Result.fail("手速慢了~已经被别人抢到了");
        }
    }

    @Override
    public Result allQuick(Integer courseId) {
        List<Quickresponse> quickresponses = quickresponseMapper.selectByCourseId(courseId);
        return Result.ok(quickresponses);
    }

    @Override
    public Result insertQuick(Quickresponse quickresponse) {
        int rows = quickresponseMapper.insert(quickresponse);
        if (rows > 0) {
            return Result.okMsg("发布抢答成功");
        }
        return Result.fail("发布抢答失败");
    }
}
